package com.bankcomm.ui.specialwidget;


import java.util.List;

import com.bankcomm.R;
import com.bankcomm.ui.BOC_SpecialShop_main_Activity;
import com.rytong.tools.utils.EventHandler;
import com.rytong.tools.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MostPopShopTypePicker extends LinearLayout {
	String bigType[] = null;
	String smallType[][] = null;
	String[] smallTypeSet;
	// 大类
	private ListView bigItemList;
	// 小类
	private ListView smallItemList;
	public BigTypeAdapter bigTypeAdapter;
	private int last_item = -1;
	protected int showPoition;
	protected int showPoitionSmall;
	protected Context lineContext;
	public SmallTypeAdapter smallTypeAdapter;
	private View outSideView;

	public MostPopShopTypePicker(Context context, String[] bigTy,
			String[][] smallTy) {
		super(context);
		lineContext = context;
		bigType = bigTy;
		smallType = smallTy;
		LayoutInflater.from(context).inflate(R.layout.type_picker_layout, this);
		bigItemList = (ListView) findViewById(R.id.bigTypeList);
		bigItemList.setOnItemClickListener(BigTypeListener);

		bigTypeAdapter = new BigTypeAdapter(context, bigType, last_item);
		bigItemList.setAdapter(bigTypeAdapter);

		smallItemList = (ListView) findViewById(R.id.smallTypeList);
		smallTypeAdapter = new SmallTypeAdapter(lineContext, smallType,
				0);
		smallItemList.setDivider(null);
		smallTypeAdapter.setSelectedPosition(0);
		smallItemList.setAdapter(smallTypeAdapter);
		smallItemList.setOnItemClickListener(smallTypeListener);
		outSideView=findViewById(R.id.outside_view);
		outSideView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
              Activity act=(Activity)getContext();
              if(act instanceof BOC_SpecialShop_main_Activity){
            	  PopupWindow aroundPW=((BOC_SpecialShop_main_Activity)act).aroundPW;
            	  if(aroundPW!=null){
            	  if (aroundPW.isShowing()) {
						aroundPW.dismiss();
					}
            	  }
              }
			}
		});
	}

	OnItemClickListener BigTypeListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			showPoition = position;
			bigTypeAdapter.setSelectedPosition(position);
			bigTypeAdapter.notifyDataSetInvalidated();
			smallItemList = (ListView) findViewById(R.id.smallTypeList);
			smallTypeAdapter = new SmallTypeAdapter(lineContext, smallType,
					showPoition);
			smallTypeAdapter.setSelectedPosition(0);
			smallItemList.setDivider(null);
			smallItemList.setAdapter(smallTypeAdapter);
			smallItemList.setOnItemClickListener(smallTypeListener);
		}
	};

	OnItemClickListener smallTypeListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			showPoitionSmall = position;
			smallTypeAdapter.setSelectedPosition(position);
			smallTypeAdapter.notifyDataSetChanged();
			smallTypeAdapter.notifyDataSetInvalidated();
			Utils.printOutToConsole("大类==========:"+smallTypeAdapter.bigPostion);
			Utils.printOutToConsole("小类==========:"+position);
			EventHandler uh = EventHandler.getUtHandler();
			Message msg = new Message();
			msg.what = EventHandler.MSG_AROUND_SEARCH;
			Bundle bundler=new Bundle();
			bundler.putInt("bigType",smallTypeAdapter.bigPostion);
			bundler.putInt("smallType",position);
			msg.setData(bundler);
			uh.eventHandler.sendMessage(msg);
			
		}
	};
	
	

	public class BigTypeAdapter extends BaseAdapter {
		Context context;
		LayoutInflater inflater;
		String[] bigtype;
		int last_item;
		int[] image;
		private int selectedPosition = -1;

		public void setSelectedPosition(int position) {
			selectedPosition = position;
		}

		// public BigTypeAdapter(Context context,String [] big,int[] image,int
		// last_item){
		// this.context = context;
		// this.bigtype = big;
		// this.image = image;
		// this.last_item = last_item;
		// inflater =
		// (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//
		// }

		public BigTypeAdapter(Context context, String[] big, int last_item) {
			this.context = context;
			this.bigtype = big;
			this.last_item = last_item;
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return bigtype.length;
		}

		@Override
		public Object getItem(int position) {
			return bigtype[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.listitem, null);
				holder = new ViewHolder();
				holder.textView = (TextView) convertView
						.findViewById(R.id.textview);
				// holder.imageView
				// =(ImageView)convertView.findViewById(R.id.imageview);
				holder.layout = (LinearLayout) convertView
						.findViewById(R.id.colorlayout);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			/*
			 * if(last_item == position){
			 * holder.textView.setBackgroundColor(R.color.bg); }
			 */
			holder.textView.setText(bigtype[position]);
			// 设置选中效果
			if (selectedPosition == position) {
				holder.textView.setTextColor(Color.rgb(45, 69, 143));

				holder.layout.setBackgroundColor(Color.rgb(190, 190, 190));
			} else {
				holder.textView.setTextColor(Color.rgb(45, 69, 143));
				holder.layout.setBackgroundColor(Color.TRANSPARENT);
			}

			// holder.imageView.setBackgroundResource(image[position]);
			return convertView;
		}

		public class ViewHolder {
			public TextView textView;
			public ImageView imageView;
			public LinearLayout layout;
		}

	}

	public class SmallTypeAdapter extends BaseAdapter {
		List<String> lst1;
		Context context;
		LayoutInflater layoutInflater;
		String[][] smalltype;
		public int bigPostion;

		private int selectedPosition = -1;

		public void setSelectedPosition(int position) {
			selectedPosition = position;
		}

		public SmallTypeAdapter(Context context, String[][] smalls, int position) {
			this.context = context;
			this.smalltype = smalls;
			layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.bigPostion = position;
		}

		@Override
		public int getCount() {
			if (smalltype[bigPostion].length != 0) {
				return smalltype[bigPostion].length;
			} else {
				return 1;
			}
		}

		@Override
		public Object getItem(int position) {

			return getItem(position);
		}

		@Override
		public long getItemId(int position) {

			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder viewHolder = null;
			if (convertView == null) {
				convertView = layoutInflater.inflate(R.layout.pick_item_layout,
						null);
				viewHolder = new ViewHolder();
				viewHolder.textView = (TextView) convertView
						.findViewById(R.id.textview1);
				viewHolder.layout = (LinearLayout) convertView
						.findViewById(R.id.colorLayout);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			String text = "";
			if (smalltype[bigPostion].length != 0) {
				try {
					text = smalltype[bigPostion][position];
				} catch (Exception e) {
					text = "nima";
				}
			} else {
               text = bigType[bigPostion];
			}

			viewHolder.textView.setText(text);
			// 设置选中效果
			if (selectedPosition == position) {
				viewHolder.textView.setTextColor(Color.WHITE);

				viewHolder.layout.setBackgroundColor(Color.rgb(127, 157, 227));
			} else {
				viewHolder.textView.setTextColor(Color.rgb(45, 69, 143));
				viewHolder.layout.setBackgroundColor(Color.TRANSPARENT);
			}

			return convertView;
		}

		public class ViewHolder {
			public TextView textView;
			public LinearLayout layout;
		}

	}
}
